<!--
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-04-25 21:35:52
 * @LastEditTime: 2022-04-25 23:03:44
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description: 
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title></title>
</head>

<body>
  <div id="test"></div>
  <script type="text/javascript" src="../js/react.development.js"></script>
  <script type="text/javascript" src="../js/react-dom.development.js"></script>
  <script type="text/javascript" src="../js/babel.min.js">
  </script>
  <script src="../js/prop-types.js"></script>
  <script type="text/babel">
    class A extends React.Component {
      // 1.首先执行构造器
      constructor() {
        super()
        this.state = { count: 0 }
        console.log('A--constructor');

      }
      add = () => {
        // 触发 shouldComponentUpdate
        this.setState({ count: this.state.count + 1 })
      }
      force = () => {
        this.setState({ count: this.state.count + 1 })
        // 触发 componentWillUpdate
        this.forceUpdate()
      }
      unmounted = () => {
        ReactDOM.unmountComponentAtNode(document.querySelector('#test'))

      }
      // 2.即将挂载----常用:开启定时器、发送请求、订阅消息
      componentWillMount() {
        console.log('A---componentWillMount');
      }
      // 4.挂载完成
      componentDidMount() {
        console.log('A---componentDidMount');
      }
      /* 3.即将更新 */
      componentWillUpdate() {
        console.log('A---componentWillUpdate');
      }
      /* 4.更新完成 */
      componentDidUpdate() {
        console.log('A---componentDidUpdate');
      }
      /* 2.是否可以更新,默认为 true */
      shouldComponentUpdate() {
        console.log('A---shouldComponentUpdate');
        return true
      }
      /*  //5.即将卸载----常用：关闭定时器、取消订阅   */
      componentWillUnmount() {
        console.log('A---componentWillUnmount');
      }
      /*  // 3.渲染  */
      render() {
        console.log('A---render');
        return (<div>
          A组件
          <div>
            <button onClick={this.add}>+1</button>
            <button onClick={this.force}>强制更新</button>
            <button onClick={this.unmounted}>卸载</button>
          </div>
          <B count={this.state.count}></B>
        </div>)
      }
    }
    class B extends React.Component {
      /* 1.接收新的 props */
      componentWillReceiveProps(props) {
        console.log('B---componentWillReceiveProps', props);
      }
      componentWillUpdate() {
        console.log('B---componentWillUpdate');
      }
      componentDidUpdate() {
        console.log('B---componentDidUpdate');
      }
      componentWillUnmount() {
        console.log('B---componentWillUnmount');
      }
      render() {
        return <div>接收A组件的值:{this.props.count}</div>
      }

    }
    ReactDOM.render(<div><A></A></div>, document.querySelector('#test'))
  </script>
</body>

</html>